﻿using AnesSystem.Models;
using AnesSystem.Services;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels.Popup
{
    public class OperationOptCtrlViewModel:BaseViewModel<OperationOptCtrlViewModel>
    {
        #region Variables
        //主刀医生
        private string _zdys = "";
        //一助
        private string _yizhu ="";
        //二助
        private string _erzhu = "";
        //三助
        private string _sanzhu = "";


        private String _disOperDoctorName = "";
        //手术医生集合
        private List<AccountModel> _operDoctor1ModelList = new List<AccountModel>();

        #endregion

        #region Constructor
        public OperationOptCtrlViewModel()
        {
            if (PatientLink.AnesthesiaRecord.OperDoctor1 != null&& PatientLink.AnesthesiaRecord.OperDoctor1.Length==24) _zdys= PatientLink.AnesthesiaRecord.OperDoctor1;
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant1 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant1.Length == 24) _yizhu = PatientLink.AnesthesiaRecord.OperDoctor1Assistant1;
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant2 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant2.Length == 24) _erzhu= PatientLink.AnesthesiaRecord.OperDoctor1Assistant2;
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant3 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant3.Length == 24) _sanzhu= PatientLink.AnesthesiaRecord.OperDoctor1Assistant3;
        }

        #endregion

        #region Properties
        public string Zdys
        {
            get
            {
                return _zdys;
            }

            set
            {
                _zdys = value;
                RaisePropertyChanged("Zdys");
            }
        }

        public string Yizhu
        {
            get
            {
                return _yizhu;
            }

            set
            {
                _yizhu = value;
                RaisePropertyChanged("Yizhu");
            }
        }

        public string Erzhu
        {
            get
            {
                return _erzhu;
            }

            set
            {
                _erzhu = value;
                RaisePropertyChanged("Erzhu");
            }
        }

        public string Sanzhu
        {
            get
            {
                return _sanzhu;
            }

            set
            {
                _sanzhu = value;
                RaisePropertyChanged("Sanzhu");
            }
        }

        public string DisOperDoctorName
        {
            get
            {
                ConvertOperDoctor1();
                if (string.IsNullOrEmpty(_disOperDoctorName)) return "";
                return _disOperDoctorName;
            }

            set
            {
                _disOperDoctorName = value;
            }
        }
        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void ConvertOperDoctor1()
        {
            _disOperDoctorName = "";
            if (PatientLink.AnesthesiaRecord.OperDoctor1 != null && PatientLink.AnesthesiaRecord.OperDoctor1.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.OperDoctor1));
                if (a != null) _disOperDoctorName += a.TrueName;
            }
            else
            {
                _disOperDoctorName += string.IsNullOrEmpty(PatientLink.AnesthesiaRecord.OperDoctor1) ? "" : (PatientLink.AnesthesiaRecord.OperDoctor1);
            }
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant1 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant1.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.OperDoctor1Assistant1));
                if (a != null) _disOperDoctorName +=","+ a.TrueName;
            }
            else
            {
                _disOperDoctorName += string.IsNullOrEmpty(PatientLink.AnesthesiaRecord.OperDoctor1Assistant1) ? "" : ("," + PatientLink.AnesthesiaRecord.OperDoctor1Assistant1);
            }
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant2 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant2.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.OperDoctor1Assistant2));
                if (a != null) _disOperDoctorName += "," + a.TrueName;
            }
            else
            {
                _disOperDoctorName += string.IsNullOrEmpty(PatientLink.AnesthesiaRecord.OperDoctor1Assistant2) ? "" : ("," + PatientLink.AnesthesiaRecord.OperDoctor1Assistant2);
            }
            if (PatientLink.AnesthesiaRecord.OperDoctor1Assistant3 != null && PatientLink.AnesthesiaRecord.OperDoctor1Assistant3.Length == 24)
            {
                AccountModel a = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(PatientLink.AnesthesiaRecord.OperDoctor1Assistant3));
                if (a != null) _disOperDoctorName += "," + a.TrueName;
            }
            else
            {
                _disOperDoctorName += string.IsNullOrEmpty(PatientLink.AnesthesiaRecord.OperDoctor1Assistant3) ? "" : ("," + PatientLink.AnesthesiaRecord.OperDoctor1Assistant3);
            }
        }
        #endregion

        #region Public/Protected Methods
        public void Keep()
        {
            Zdys = Util.ExtensionMethods.CheckAccount(_zdys, "手术医生");
            PatientLink.AnesthesiaRecord.OperDoctor1 = Zdys;

            Yizhu = Util.ExtensionMethods.CheckAccount(_yizhu, "手术医生");
            PatientLink.AnesthesiaRecord.OperDoctor1Assistant1 = Yizhu;

            Erzhu = Util.ExtensionMethods.CheckAccount(_erzhu, "手术医生");
            PatientLink.AnesthesiaRecord.OperDoctor1Assistant2 = Erzhu;

            Sanzhu = Util.ExtensionMethods.CheckAccount(_sanzhu, "手术医生");
            PatientLink.AnesthesiaRecord.OperDoctor1Assistant3 = Sanzhu;

            PatientLinkService.Instance.AddUpdate(PatientLink);
            RaisePropertyChanged("DisOperDoctorName");
        }

        #endregion

        #region EventHandlers

        #endregion

    }
}
